func threeSum(nums []int) [][]int {
    n := len(nums)
    ans := make([][]int, 0)
    sort.Ints(nums)
    for i := 0; i<n-2; i++ {
        n1 := nums[i]
        if n1 > 0 {
            break
        }
        if i > 0 && n1 == nums[i-1] {
            continue
        }
        l, r := i+1, n-1
        for l < r {
            n2, n3 := nums[l], nums[r]
            sum := n1 + n2 + n3
            if sum == 0 {
                ans = append(ans, []int{n1, n2, n3})
                for l < n-1 && nums[l] == n2 {
                    l++
                }
                for r > 0 && nums[r] == n3 {
                    r--
                }
            } else if sum > 0 {
                r--
            } else {
                l++
            }
        }
    }

    return ans

}